// http 模块
layui.define(['layer'], function (exports) {

    var obj = {
        post: function (url, data, fn, $btn = null, refresh = false) {
            var $ = layui.$,
                layer = layui.layer
            if ($btn) {
                var txt = $btn.text()
                $btn.text("提交中...").attr("disabled", true).addClass("layui-disabled")
            }

            var index = layer.load(3, {
                shade: [0.1, '#000'],
                time: 10 * 1000
            })
            $.ajax({
                url: url,
                type: 'POST',
                data: data,
                success: function (res) {
                    if ($btn) {
                        $btn.text(txt).attr("disabled", false).removeClass("layui-disabled")
                    }
                    layer.close(index)
                    if (res.code == 0) {
                        layer.msg(res.message, {
                            offset: '45px',
                            icon: 1,
                            time: 800,
                        }, function () {
                            if (refresh || res.refresh)
                                window.location.reload()
                        })
                    } else layer.msg(res.message, {
                        offset: '45px',
                        icon: 5
                    })
                    typeof fn == "function" && fn(res)
                },
                error: function () {
                    layer.msg('服务器错误，请联系管理员！')

                }
            })
        },
        get: function (url, title = '系统提示', area = ['850px', '600px'], fn) {
            var $ = layui.$
            $.ajax({
                url: url,
                success: function (html) {
                    if (html.code && html.code != 0) {
                        layer.msg(html.message, {
                            offset: '45px',
                            icon: 5
                        })
                    } else {
                        layer.open({
                            type: 1,
                            maxmin: true,
                            title: title,
                            content: html,
                            skin: 'layui-layer-admin',
                            area: area,
                            shadeClose: true,
                        })
                    }
                    typeof fn == "function" && fn(res)

                },
                error: function () {
                    layer.msg('服务器错误，请联系管理员！')

                }
            })
        }
    }

    //输出test接口
    exports('http', obj)
})